﻿//-----------------------------------------------------------------------------------------//
//-     Writen by Raymond Honderdors
//-     2013-05-14
//-
//-     Code can be used as is, no warrenty
//-     https://solutionmanagementki.codeplex.com/
//-----------------------------------------------------------------------------------------//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using SMK_SSAS;



namespace SMK_Treeview
{
    public partial class treeview: UserControl
    {

        #region Designer Parts
        private System.Windows.Forms.ContextMenuStrip contextMenuStripASActivities;
        private System.Windows.Forms.ToolStripMenuItem cancelConnectionToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem cancelCommandToolStripMenuItem;
        private void InitializeComponentint()
        {
            this.SuspendLayout();
            this.contextMenuStripASActivities = new System.Windows.Forms.ContextMenuStrip(this.components);
            this.cancelConnectionToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.cancelCommandToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.contextMenuStripASActivities.SuspendLayout();
            
            // 
            // contextMenuStripASActivities
            // 
            this.contextMenuStripASActivities.Items.AddRange
                            (new System.Windows.Forms.ToolStripItem[] 
                            {
                                this.cancelConnectionToolStripMenuItem
                                ,this.cancelCommandToolStripMenuItem
                            }
                            );
            this.contextMenuStripASActivities.Name = "contextMenuStripASActivities";
            this.contextMenuStripASActivities.Size = new System.Drawing.Size(153, 48);
            // 
            // cancelSessionToolStripMenuItem
            // 
            this.cancelConnectionToolStripMenuItem.Name = "cancelSessionToolStripMenuItem";
            this.cancelConnectionToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.cancelConnectionToolStripMenuItem.Text = "Cancel Session";
            this.cancelConnectionToolStripMenuItem.Click += new System.EventHandler(this.cancelConnectionToolStripMenuItem_Click);
            // 
            // cancelCommandToolStripMenuItem
            // 
            this.cancelCommandToolStripMenuItem.Name = "cancelCommandToolStripMenuItem";
            this.cancelCommandToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.cancelCommandToolStripMenuItem.Text = "Cancel Command";
            this.cancelCommandToolStripMenuItem.Click += new System.EventHandler(this.cancelCommandToolStripMenuItem_Click);
                        
            this.contextMenuStripASActivities.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        private void cancelCommandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListView lsv = gtvp.winlist;
            string amoCommand = string.Empty;
            string[] sList;
            if (lsv.SelectedItems.Count == 1)
            {

                //<Cancel xmlns="http://schemas.microsoft.com/analysisservices/2003/engine";> 
                //    <ConnectionID>[CID]</ConnectionID> 
                //    <SessionID>[SID]</SessionID> 
                //    <SPID>[SPID]</SPID> 
                //    <CancelAssociated>[true/false]</CancelAssociated> 
                //</Cancel>

                //only support 1 item at the time
                sList = pathlist(lsv.SelectedItems[0].Name);
                amocon = (AMO_Server)treelevel0[sList[0] + '\\' + sList[1]];
                AMO_Key amok = (AMO_Key)treelevel2[lsv.SelectedItems[0].Name];
                if (amok.LevelObject is AMO_SessionData)
                {
                    AMO_SessionData amos = (AMO_SessionData)amok.LevelObject;
                    amoCommand = "<Cancel xmlns='http://schemas.microsoft.com/analysisservices/2003/engine'>  <SPID>" + amos.SESSION_SPID + "</SPID>   <CancelAssociated>true</CancelAssociated> </Cancel>";
                    if (amoCommand != string.Empty)
                    {
                        try
                        {
                            amocon.amoserver().Execute(amoCommand);
                            // Connection remain open
                        }
                        catch (Exception exc)
                        {
                            throw new Exception("Failed to execute command", exc);
                        }
                    }

                }
            }
        }

        private void cancelConnectionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ListView lsv = gtvp.winlist;
            string amoCommand = string.Empty;
            string[] sList;
            if (lsv.SelectedItems.Count == 1)
            {

                //<Cancel xmlns="http://schemas.microsoft.com/analysisservices/2003/engine";> 
                //    <ConnectionID>[CID]</ConnectionID> 
                //    <SessionID>[SID]</SessionID> 
                //    <SPID>[SPID]</SPID> 
                //    <CancelAssociated>[true/false]</CancelAssociated> 
                //</Cancel>

                //only support 1 item at the time
                sList = pathlist(lsv.SelectedItems[0].Name);
                amocon = (AMO_Server)treelevel0[sList[0] + '\\' + sList[1]];
                amoCommand = "<Cancel xmlns='http://schemas.microsoft.com/analysisservices/2003/engine'>  <ConnectionID>" + sList[4] + "</ConnectionID>   <CancelAssociated>true</CancelAssociated> </Cancel>";
                if (amoCommand != string.Empty)
                {
                    try
                    {
                        amocon.amoserver().Execute(amoCommand);
                        treelevel2.Remove(lsv.SelectedItems[0].Name);
                        TreeNode tn = this.treeView1.Nodes.Find(lsv.SelectedItems[0].Name, true)[0];
                        tn.Remove();
                        lsv.Items.Remove(lsv.SelectedItems[0]);

                    }
                    catch (Exception exc)
                    {
                        throw new Exception("Failed to execute command", exc);
                    }
                }
            }
        }


        #endregion

        private SMK_SSAS.AMO_Server amocon = null;
        private Treeview_Properties gtvp = null;

        //private SortedList<string, SMK_SSAS.AMO_Server> amoconlist = new SortedList<string, SMK_SSAS.AMO_Server>();

        public Boolean RightToLeftLayout
        { get{return this.treeView1.RightToLeftLayout;}
            set { this.treeView1.RightToLeftLayout = value; }
        }


        /// <summary>
        /// treelevel0 holds all server connections
        /// </summary>
        private SortedList<string, object> treelevel0 = new SortedList<string, object>(); 
        /// <summary>
        /// treelevel1 holds all server object types (level 1)
        /// SSAS: Databases / Traces / Server Assemblies / Sever Roles / User Sessions
        /// </summary>
        private SortedList<string, object> treelevel1 = new SortedList<string, object>();
        /// <summary>
        /// treelevel2 holds all level1 object types (level 2)
        /// </summary>
        private SortedList<string, object> treelevel2 = new SortedList<string, object>();
        private SortedList<string, object> treelevel3 = new SortedList<string, object>();
        private SortedList<string, object> treelevel4 = new SortedList<string, object>();
        private SortedList<string, object> treelevel5 = new SortedList<string, object>();

        private const string AnalysisService = "Analysis Service";
        private const string DataBases = "Databases";
        private const string Traces = "Traces";
        private const string Roles = "Roles";
        private const string Assemblies = "Assemblies";
        private const string Accounts = "Accounts";
        private const string Cubes = "Cubes";
        private const string DatabasePermissions = "Database Permissions";
        private const string DataSources = "DataSources";
        private const string DataSourcesPermissions = "DataSources Permissions";
        private const string DataSourceViews = "DataSourceViews";
        private const string Dimensions = "Dimensions";
        private const string Atributes = "Atributes";
        private const string Hierarchies = "Hierarchies";
        private const string Relations = "Relations";
        private const string MiningStructures = "Mining Structures";
        private const string Translations = "Translations";
        private const string Members = "Members";
        private const string Sessions = "Activities"; // Renamed as Activities

        public delegate void TreeviewSelected(object sender, Treeview_Properties key);
        public delegate void ServerAdded(string servername);

        public event TreeviewSelected tvs;
        public event ServerAdded tvserveradded;

        public treeview()
        {
            InitializeComponent();
            InitializeComponentint();
        }

        public void Addserver(string Servername,string connectionType)
        {
            if (Servername == ".")
            {
                Servername = Environment.MachineName;
            }
            TreeNode trnServer = null;
            TreeNode trnDummy = new TreeNode();

            if (this.treeView1.Nodes.ContainsKey(Servername)) 
            {
                trnServer = this.treeView1.Nodes[Servername];
            }
            else
            {
                trnServer = new TreeNode();
                trnServer.Name = Servername;
                trnServer.Text = Servername;
                trnServer.SelectedImageKey = "Server.ico";
                trnServer.ImageKey = "Server.ico";
                this.treeView1.Nodes.Add(trnServer);
            }

            if (!trnServer.Nodes.ContainsKey(Servername + treeView1.PathSeparator + connectionType))
            {
                AMO_Key amoK = null;
                AddTreeLevel(connectionType, trnServer, Servername + treeView1.PathSeparator + connectionType, connectionType, false, amoK, "Server_SSAS.ico");
                try
                {
                    if (connectionType == AnalysisService)
                    {
                        amocon = new AMO_Server();
                        amocon.AMOConnection(Servername);
                        if (!treelevel0.ContainsKey(Servername + treeView1.PathSeparator + connectionType))
                        {
                            treelevel0.Add(Servername + treeView1.PathSeparator + connectionType, amocon);
                        }
                    }
                    tvserveradded(amocon.ServerName());
                }
                catch (Exception ex)
                {
                    throw new Exception("Load treeview", ex);
                }
            }
            else
            { 
                //Do Nothing Connection already exists
            }
            this.treeView1.Sort();         
        }
        private void AddTreeLevel(string connectionType, TreeNode Parent, string Key, string Text, Boolean Lowestlevel = false, AMO_Key amoK = null, string imagekey = "Folder.ico")
        {
            if(Parent.Nodes.ContainsKey("Dummy"))
            {
                Parent.Nodes.Clear();
            }

                TreeNode trnDummy = new TreeNode();
                trnDummy.Text = "Dummy";
                trnDummy.Name = "Dummy";

            if (connectionType == AnalysisService)
            {
                if (!Parent.Nodes.ContainsKey(Key))
                {
                    TreeNode trnlvl1 = new TreeNode();
                    trnlvl1.Name = Key;
                    trnlvl1.Text = Text;
                    trnlvl1.ImageKey = imagekey;
                    trnlvl1.SelectedImageKey = imagekey;
                    if (amoK != null)
                    {
                        if (!amoK.IsActive)
                        { 
                            trnlvl1.ForeColor = Color.Gray;
                        }
                    }
                    if (!Lowestlevel)
                    {
                        trnlvl1.Nodes.Add(trnDummy);
                    }
                    Parent.Nodes.Add(trnlvl1);
                }
            }
        }
        private void AddTreeLevelData(SortedList<string, object> slist, string Key, Object lObject)
        {
            if (slist.ContainsKey(Key))
            {
                slist.Remove(Key);
            }
            slist.Add(Key, lObject);
        }
        private string[] pathlist(string path)
        { 
                return path.Split(new string[] { this.treeView1.PathSeparator }, StringSplitOptions.None);
        }

        #region TreeLevel 1
        private void AddTreeLevel1AsGeneral(TreeNode ben, AMO_Key amok, string[] sList, Boolean Lowestlevel = false, string imagekey = "Folder.ico")
        {
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, Lowestlevel, amok, imagekey);
            //AddTreeLevelData(treelevel1, ben.Name + treeView1.PathSeparator + amok.ID, amok);
        }
        private void AddTreeLevel1ASCon(TreeNode ben, string[] sList)
        {
            amocon = (AMO_Server)treelevel0[ben.Name];
            AMO_Key amok = new AMO_Key();

            amok.ID = DataBases;
            amok.Name = DataBases;
            AddTreeLevel1AsGeneral(ben, amok, sList, !amocon.HasDataBases()); //"ASDatabase.ico"

            amok.ID = Assemblies;
            amok.Name = Assemblies;
            AddTreeLevel1AsGeneral(ben, amok, sList, !amocon.HasAssemblies()); //"Assembly.ico"

            amok.ID = Traces;
            amok.Name = Traces;
            AddTreeLevel1AsGeneral(ben, amok, sList, !amocon.HasTraces(), "performance.ico");

            amok.ID = Roles;
            amok.Name = Roles;
            AddTreeLevel1AsGeneral(ben,amok,sList,!amocon.HasRoles(),"Role.ico");

            amok.ID = Sessions;
            amok.Name = Sessions;
            AddTreeLevel1AsGeneral(ben, amok, sList, !amocon.HasSessions(), "Activity.ico");

        }
        private void AddTreeLevel1(TreeNode ben, string[] sList)
        {

            if (treelevel0.ContainsKey(ben.Name))
            {
                if (treelevel0[ben.Name] is SMK_SSAS.AMO_Server)
                {
                    AddTreeLevel1ASCon(ben, sList);
                }
            }

        }
        #endregion

        #region Treelevel 2

        private void AddTreeLevel2AsGeneral(TreeNode ben, AMO_Key amok, string[] sList, Boolean Lowestlevel = false, string imagekey="Folder.ico")
        {
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name,Lowestlevel,amok,imagekey);
            AddTreeLevelData(treelevel1, ben.Name + treeView1.PathSeparator + amok.ID, amok);
        }

        private void AddTreeLevel2AsServer(TreeNode ben, string[] sList)
        {
            amocon = (AMO_Server)treelevel0[ben.Parent.Name];
            if (sList[2] == DataBases)
            {
                SortedList<string, AMO_Key> sdbList = amocon.DataBases();
                foreach (AMO_Key amok in sdbList.Values)
                {
                    AddTreeLevel2AsGeneral(ben, amok, sList, false, "ASDatabase.ico"); //  "ASDatabase.ico"
                }
            }
            else if (sList[2] == Roles)
            {
                SortedList<string, AMO_Key> sdbList = amocon.Roles();
                foreach (AMO_Key amok in sdbList.Values)
                {
                    AddTreeLevel2AsGeneral(ben, amok, sList, false ,"users.ico");
                }
            }
            else if (sList[2] == Traces)
            {
                SortedList<string, AMO_Key> sdbList = amocon.Traces();
                foreach (AMO_Key amok in sdbList.Values)
                {
                    AddTreeLevel2AsGeneral(ben, amok, sList, true, "Trace.ico");
                }
            }
            else if (sList[2] == Assemblies)
            {
                SortedList<string, AMO_Key> sdbList = amocon.Assemblies();
                foreach (AMO_Key amok in sdbList.Values)
                {
                    AddTreeLevel2AsGeneral(ben, amok, sList, true, "Assembly.ico"); //"Assembly.ico"
                }
            }
            else if (sList[2] == Sessions)
            {
                SortedList<string, AMO_Key> sdbList = amocon.Users();
                foreach (AMO_Key amok in sdbList.Values)
                {
                    AddTreeLevel2AsGeneral(ben, amok, sList, false, "user.ico");
                }
            }
        }
        private void AddTreeLevel2(TreeNode ben, string[] sList)
        {

            if (treelevel0.ContainsKey(ben.Parent.Name))
            {
                if (treelevel0[ben.Parent.Name] is SMK_SSAS.AMO_Server)
                {
                    AddTreeLevel2AsServer(ben, sList);
                }
            }

        }
        #endregion

        #region TreeLevel 3

        private void AddTreeLevel3AsGeneral(TreeNode ben, AMO_Key amok, string[] sList, Boolean Lowestlevel = false, string imagekey = "Folder.ico")
        {
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, Lowestlevel,amok, imagekey);
            AddTreeLevelData(treelevel2, ben.Name + treeView1.PathSeparator + amok.ID, amok);
        }

        private void AddTreeLevel3ASDataBase(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Accounts, Accounts, !amodb.HasAccounts());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Assemblies, Assemblies, !amodb.HasAssemblies());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Cubes, Cubes, !amodb.HasCubes());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Roles, Roles, !amodb.HasRoles(), null, "Role.ico");
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DatabasePermissions, DatabasePermissions, !amodb.HasDatabasePermissions());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSources, DataSources, !amodb.HasDataSources());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourceViews, DataSourceViews, !amodb.HasDataSourceViews());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Dimensions, Dimensions, !amodb.HasDimensions());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + MiningStructures, MiningStructures, !amodb.HasMiningStructures());
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Translations, Translations, !amodb.HasTranslations());
        }
        private void AddTreeLevel3ASRoles(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.FullPath];
            AMO_Roles amorole = (AMO_Roles)amok.LevelObject;
            if (amorole != null)
            {
                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Members, Members, !amorole.HasMembers());
            }
        }
        private void AddTreeLevel3ASSessions(TreeNode ben, string[] sList)
        {
            SortedList<string, AMO_Key> sdbList = amocon.Sessions(sList[3].Replace("/", "\\"));
            foreach (AMO_Key amok in sdbList.Values)
            {
                AddTreeLevel3AsGeneral(ben, amok, sList, true, "Session.ico");
            }
        }

        private void AddTreeLevel3(TreeNode ben, string[] sList)
        {
            if (treelevel1.ContainsKey(ben.FullPath))
            {
                if (sList[1] == AnalysisService)
                {
                    if (sList[2] == DataBases)
                    {
                        AddTreeLevel3ASDataBase(ben, sList); 
                    }
                    else if (sList[2] == Roles)
                    {
                        AddTreeLevel3ASRoles(ben,sList);
                    }
                    else if (sList[2] == Sessions)
                    {
                        AddTreeLevel3ASSessions(ben, sList);
                    }
                }
            }
        }
        #endregion
        
        #region TreeLevel 4

        private void AddTreeLevel4AsGeneral(TreeNode ben, AMO_Key amok, string[] sList, Boolean Lowestlevel = false, string imagekey = "Folder.ico")
        {
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, Lowestlevel, amok, imagekey);
            AddTreeLevelData(treelevel2, ben.Name + treeView1.PathSeparator + amok.ID, amok);
        }

        private void AddTreeLevel4AS(TreeNode ben, string[] sList)
        {
            if (sList[2] == DataBases)
            {
                AddTreeLevel4ASDataBases(ben, sList);
            }
            else if (sList[2] == Roles)
            {
                AddTreeLevel4ASRoles(ben, sList);
            }
        }
        private void AddTreeLevel4ASDataBases(TreeNode ben, string[] sList)
        {
            if (sList[4] == Dimensions)
            {
                AddTreeLevel4ASDimensions(ben, sList);
            }
            else if (sList[4] == Cubes)
            {
                AddTreeLevel4ASCubes(ben, sList);
            }
            else if (sList[4] == DataSources)
            {
                AddTreeLevel4ASDataSources(ben, sList);
            }
            else if (sList[4] == DataSourceViews)
            {
                AddTreeLevel4ASDataSourceViews(ben, sList);
            }
            else if (sList[4] == Assemblies)
            {
                AddTreeLevel4ASAssemblies(ben, sList);
            }
            else if (sList[4] == MiningStructures)
            {
                AddTreeLevel4ASMiningStructures(ben, sList);
            }
            else if (sList[4] == Roles)
            {
                AddTreeLevel4ASDatabaseRoles(ben, sList);
            }
            else if (sList[4] == Accounts)
            {
                AddTreeLevel4ASAccounts(ben, sList);
            }
            else if (sList[4] == Translations)
            {
                AddTreeLevel4ASTranslations(ben, sList);
            }
        }
        private void AddTreeLevel4ASRoles(TreeNode ben, string[] sList)
        {
            if (sList[4] == Members)
            {
                AddTreeLevel4ASRoleMembers(ben, sList);
            }
        }
        private void AddTreeLevel4(TreeNode ben, string[] sList)
        {
            if (treelevel1.ContainsKey(ben.Parent.FullPath))
            {
                if (sList[1] == AnalysisService)
                {
                    AddTreeLevel4AS(ben, sList);
                }
            }
        }
        private void AddTreeLevel4ASDimensions(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.DatabaseDimensions();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "Dimension.ico");
                }
            }
        }
        private void AddTreeLevel4ASCubes(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.Cubes();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, false,"cube.ico");
                }
            }
        }
        private void AddTreeLevel4ASDataSources(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.DataSources();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "DataSource.ico");
                }
            }
        }
        private void AddTreeLevel4ASDataSourceViews(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.DataSourceviews();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "DataSourceview.ico");
                }
            }
        }
        private void AddTreeLevel4ASAssemblies(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.Assemblies();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "Assembly.ico");
                }
            }
        }
        private void AddTreeLevel4ASMiningStructures(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.DatabaseMiningStructures();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "MiningModel.ico");
                }
            }
        }
        private void AddTreeLevel4ASDatabaseRoles(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.Roles();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true,"Roles.ico");
                }
            }
        }
        private void AddTreeLevel4ASAccounts(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.Accounts();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true);
                }
            }
        }
        private void AddTreeLevel4ASTranslations(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                SortedList<string, AMO_Key> sdbList = amodb.DatabaseTranslations();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true, "translation.ico");
                }
            }
        }

        private void AddTreeLevel4ASRoleMembers(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.FullPath];
            AMO_Roles amorole = (AMO_Roles)amok.LevelObject;
            if (amorole != null)
            {
                SortedList<string, AMO_Key> sdbList = amorole.Members();
                foreach (AMO_Key amok1 in sdbList.Values)
                {
                    AddTreeLevel4AsGeneral(ben, amok1, sList, true,"user.ico");
                }
            }
        }
        #endregion
        #region TreeLevel 5
        private void AddTreeLevel5AsGeneral(TreeNode ben, AMO_Key amok, string[] sList, Boolean Lowestlevel = false, string imagekey = "Folder.ico")
        {
            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, Lowestlevel, amok, imagekey);
            AddTreeLevelData(treelevel3, ben.Name + treeView1.PathSeparator + amok.ID, amok);
        }
        private void AddTreeLevel5(TreeNode ben, string[] sList)
        {
            if (treelevel1.ContainsKey(ben.Parent.Parent.FullPath))
            {
                if (sList[1] == AnalysisService)
                {
                    AddTreeLevel5AS(ben, sList);
                }
            }
        }
        private void AddTreeLevel5AS(TreeNode ben, string[] sList)
        {
            if (sList[2] == DataBases)
            {
                AddTreeLevel5ASDataBases(ben, sList);
            }
            else if (sList[2] == Roles)
            {
                //AddTreeLevel5ASRoles(ben, sList);
            }
        }
        private void AddTreeLevel5ASDataBases(TreeNode ben, string[] sList)
        {
            if (sList[4] == Dimensions)
            {
                //AddTreeLevel5ASDimensions(ben, sList);
            }
            else if (sList[4] == Cubes)
            {
                AddTreeLevel5ASCubes(ben, sList);
            }
            else if (sList[4] == DataSources)
            {
                //AddTreeLevel5ASDataSources(ben, sList);
            }
            else if (sList[4] == DataSourceViews)
            {
                //AddTreeLevel5ASDataSourceViews(ben, sList);
            }
            else if (sList[4] == Assemblies)
            {
                //AddTreeLevel5ASAssemblies(ben, sList);
            }
            else if (sList[4] == MiningStructures)
            {
                //AddTreeLevel5ASMiningStructures(ben, sList);
            }
            else if (sList[4] == Roles)
            {

                //AddTreeLevel5ASDatabaseRoles(ben, sList);
            }
            else if (sList[4] == Accounts)
            {
                //AddTreeLevel5ASAccounts(ben, sList);
            }
            else if (sList[4] == Translations)
            {
                //AddTreeLevel5ASTranslations(ben, sList);
            }
        }
        private void AddTreeLevel5ASCubes(TreeNode ben, string[] sList)
        {
            AMO_Key amok = (AMO_Key)treelevel1[ben.Parent.Parent.FullPath];
            AMO_Database amodb = (AMO_Database)amok.LevelObject;
            if (amodb != null)
            {
                if(treelevel2.ContainsKey(ben.FullPath))
                {
                    AMO_Key amok1 = (AMO_Key)treelevel2[ben.FullPath];
                    if (amok1.LevelObject is AMO_Database_Cube)
                    {
                        AMO_Database_Cube amodc = (AMO_Database_Cube)amok1.LevelObject;
                        AMO_Key amok2 = new AMO_Key();

                        amok2.Name = "Actions";
                        amok2.ID = "Actions";
                        AddTreeLevel5AsGeneral(ben, amok2, sList,!amodc.HasCubeActions(), "cube.ico");
                        amok2.Name = Dimensions;
                        amok2.ID = Dimensions;
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubeDimensions(), "cube.ico");
                        amok2.Name = "KPIs";
                        amok2.ID = "KPIs";
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubeKPIs(), "cube.ico");
                        amok2.Name = "MdxScript";
                        amok2.ID = "MdxScript";
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubeMdxScripts(), "cube.ico");
                        amok2.Name = "MeasureGroups";
                        amok2.ID = "MeasureGroups";
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubeMeasureGroups(), "cube.ico");
                        amok2.Name = "Permissions";
                        amok2.ID = "Permissions";
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubePermissions(), "cube.ico");
                        amok2.Name = "Perspectives";
                        amok2.ID = "Perspectives";
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubePerspectives(), "cube.ico");
                        amok2.Name = Translations;
                        amok2.ID = Translations;
                        AddTreeLevel5AsGeneral(ben, amok2, sList, !amodc.HasCubeTranslations(), "cube.ico");
                    }
                }
                //SortedList<string, AMO_Key> sdbList = amodb.Cubes();
                //foreach (AMO_Key amok1 in sdbList.Values)
                //{
                //    AddTreeLevel5AsGeneral(ben, amok1, sList, true, "cube.ico");
                //}
            }
        }
        #endregion

        #region Treeview Expand
        private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            if (e.Action == TreeViewAction.Expand)
            {
                TreeNode ben = e.Node;
                string[] sList = pathlist(ben.FullPath);
                if (ben.Level == 0) //server level
                {
                    //Do nothing
                }
                else if (ben.Level == 1)
                {
                    AddTreeLevel1(ben, sList);
                }
                else if (ben.Level == 2)
                {
                    AddTreeLevel2(ben, sList);
                }
                else if (ben.Level == 3)
                {
                    AddTreeLevel3(ben, sList);
                }
                else if (ben.Level == 4)
                {
                    AddTreeLevel4(ben, sList);
                }
                else if (ben.Level == 5)
                {
                    AddTreeLevel5(ben, sList);
                }
            }
            else
            { 
                //Do nothing
            }
        }
        private void treeView1_AfterExpand(object sender, TreeViewEventArgs e)
        {
            TreeNode ben = e.Node;
            if (ben.ImageKey == "Folder.ico")
            {
                ben.ImageKey = "Folderopen.ico";
                ben.SelectedImageKey = "Folderopen.ico";

            }
        }
        #endregion

        #region Treeview Collapse
        private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.Collapse)
            {
                TreeNode ben = e.Node;
                if (ben.Level >= 1)
                {
                    ben.Nodes.Clear();
                    TreeNode trnDummy = new TreeNode();
                    trnDummy.Text = "Dummy";
                    trnDummy.Name = "Dummy";
                    ben.Nodes.Add(trnDummy);
                }
                if (ben.Level < 1)
                {
                    ben.Collapse(false);
                }
                if (ben.ImageKey == "Folderopen.ico")
                {
                    ben.ImageKey = "Folder.ico";
                    ben.SelectedImageKey = "Folder.ico";

                }
            }
        }
        #endregion

        //private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        //{
        //    if (e.Action == TreeViewAction.Expand)
        //    {
                
        //        TreeNode ben = e.Node;
        //        string[] sList = pathlist(ben.FullPath);
        //        //Level 0 : Server Level
        //        if (ben.Level == 0)
        //        {
        //            AddTreeLevel0(ben, sList);
        //        }
        //        if (ben.Level == 1)
        //        {
        //            AddTreeLevel1(ben, sList);
        //        }
        //        //Level 1 : Connection Type Level (Analysis Service)
        //        //else if (ben.Level == 1)
        //        //{
        //        //    AddTreeLevel1(ben, sList);
        //        //}
        //        //Level 2 : Server Object Level (Analysis Service)
        //        else if (ben.Level == 2)
        //        {
        //            //string[] sList = ben.FullPath.Split(new string[] { this.treeView1.PathSeparator }, StringSplitOptions.None);
                    
        //            if (amoconlist.ContainsKey(sList[0]))
        //            {
        //                amocon = amoconlist[sList[0]];
        //                if (sList[1] == AnalysisService)
        //                {
        //                    if (sList[2] == DataBases)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.DataBases();
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                        }
        //                    }
        //                    else if (sList[2] == Roles)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.Roles();
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                        }
        //                    }
        //                    else if (sList[2] == Traces)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.Traces();
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true);
        //                        }
        //                    }
        //                    else if (sList[2] == Assemblies)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.Assemblies();
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true);
        //                        }
        //                    }
        //                    else if (sList[2] == Sessions)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.Users();
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                        }
        //                    }
        //                }
        //            }

        //        }
        //        //Level 3 : 
        //        else if (ben.Level == 3)
        //        {
        //           // string[] sList = ben.FullPath.Split(new string[] { this.treeView1.PathSeparator }, StringSplitOptions.None);
        //            if (amoconlist.ContainsKey(sList[0]))
        //            {
        //                amocon = amoconlist[sList[0]];
        //                if (sList[1] == AnalysisService)
        //                {
        //                    if (sList[2] == DataBases)
        //                    {
        //                        AMO_Database amodb = null;
        //                        amodb = new AMO_Database();
        //                        amodb.SetAMODatabase(amoconlist[sList[0]], sList[3]);
        //                        if (amodb != null)
        //                        {
        //                            if (amodb.HasAccounts())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Accounts, Accounts);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Accounts, Accounts,true);
        //                            }
        //                            if (amodb.HasAssemblies())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Assemblies, Assemblies);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Assemblies, Assemblies, true);
        //                            }
        //                            if (amodb.HasCubes())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Cubes, Cubes);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Cubes, Cubes, true);
        //                            }
        //                            if (amodb.HasRoles())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Roles, Roles);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Roles, Roles, true);
        //                            }
        //                            if (amodb.HasDatabasePermissions())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DatabasePermissions, DatabasePermissions);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DatabasePermissions, DatabasePermissions, true);
        //                            }
        //                            if (amodb.HasDataSources())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSources, DataSources);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSources, DataSources, true);
        //                            }
        //                            if (amodb.HasDataSourceViews())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourceViews, DataSourceViews);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourceViews, DataSourceViews, true);
        //                            }
        //                            if (amodb.HasDimensions())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Dimensions, Dimensions);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Dimensions, Dimensions, true);
        //                            }
        //                            if (amodb.HasMiningStructures())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + MiningStructures, MiningStructures);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + MiningStructures, MiningStructures, true);
        //                            }
        //                            if (amodb.HasTranslations())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Translations, Translations);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Translations, Translations, true);
        //                            }
        //                        }
        //                    }
        //                    if (sList[2] == Roles)
        //                    {
        //                        AMO_Roles amorole = null;
        //                        amorole = new AMO_Roles();
        //                        amorole.SetAMORole(amoconlist[sList[0]], sList[3]);
        //                        if (amorole != null)
        //                        {
        //                            if (amorole.HasMembers())
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Members, Members);
        //                            }
        //                            else
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + Members, Members, true);
        //                            }
        //                        }
        //                    }
        //                    if (sList[2] == Sessions)
        //                    {
        //                        SortedList<string, AMO_Key> sdbList = amocon.Sessions(sList[3].Replace("/","\\"));
        //                        foreach (AMO_Key amok in sdbList.Values)
        //                        {
        //                            AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true, amok);
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        //Level 4 : Databases / Roles
        //        else if (ben.Level == 4)
        //        {
        //            //string[] sList = ben.FullPath.Split(new string[] { this.treeView1.PathSeparator }, StringSplitOptions.None);
        //            if (amoconlist.ContainsKey(sList[0]))
        //            {
        //                if (sList[1] == AnalysisService)
        //                {
        //                    if (sList[2] == DataBases)
        //                    {
        //                        AMO_Database amodb = null;
        //                        amodb = new AMO_Database();
        //                        amodb.SetAMODatabase(amoconlist[sList[0]], sList[3]);
        //                        if (amodb != null)
        //                        {
        //                            if (sList[4] == Roles)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.Roles();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                                }
        //                            }
        //                            if (sList[4] == Accounts)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.Accounts();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                                }
        //                            }
        //                            if (sList[4] == Assemblies)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.Assemblies();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name,true);
        //                                }
        //                            }
        //                            if (sList[4] == Cubes)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.Cubes();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                                }
        //                            }
        //                            if (sList[4] == DataSources)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.DataSources();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name);
        //                                }
        //                            }
        //                            if (sList[4] == DataSourceViews)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.DataSourceviews();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name,true);
        //                                }
        //                            }
        //                            if (sList[4] == DatabasePermissions)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.DatabasePermissions();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true);
        //                                }
        //                            }
        //                            if (sList[4] == Dimensions)
        //                            {
        //                                SortedList<string, AMO_Key> sdbList = amodb.DatabaseDimensions();
        //                                foreach (AMO_Key amok in sdbList.Values)
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true);
        //                                }
        //                            }
        //                        }
        //                    }
        //                    if (sList[2] == Roles)
        //                    {
        //                        AMO_Roles amorole = null;
        //                        amorole = new AMO_Roles();
        //                        amorole.SetAMORole(amoconlist[sList[0]], sList[3]);
        //                        if (amorole != null)
        //                        {
        //                            SortedList<string, AMO_Key> sdbList = amorole.Members();
        //                            foreach (AMO_Key amok in sdbList.Values)
        //                            {
        //                                AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name, true);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        else if (ben.Level == 5)
        //        {
        //           // string[] sList = ben.FullPath.Split(new string[] { this.treeView1.PathSeparator }, StringSplitOptions.None);
        //            if (amoconlist.ContainsKey(sList[0]))
        //            {
        //                if (sList[1] == AnalysisService)
        //                {
        //                    if (sList[2] == DataBases)
        //                    {
        //                        AMO_Database amodb = null;
        //                        amodb = new AMO_Database();
        //                        amodb.SetAMODatabase(amoconlist[sList[0]], sList[3]);
        //                        if (amodb != null)
        //                        {
        //                            if (sList[4] == Accounts)
        //                            {
        //                                AMO_Database_Accounts  amoaccounts = null;
        //                                amoaccounts = new AMO_Database_Accounts();
        //                                amoaccounts.SetAMODatabaseAccount(amodb, sList[5]);
        //                                if (amoaccounts != null)
        //                                {
        //                                    SortedList<string, AMO_Key> sdbList = amoaccounts.Aliases();
        //                                    foreach (AMO_Key amok in sdbList.Values)
        //                                    {
        //                                        AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + amok.ID, amok.Name,true);
        //                                    }
        //                                }
        //                            }
        //                            if (sList[4] == DataSources)
        //                            {
        //                                AMO_Database_DataSource amodbdatasource = null;
        //                                amodbdatasource = new AMO_Database_DataSource();
        //                                amodbdatasource.SetAMODatabaseDataSource(amodb, sList[5]);
        //                                if (amodbdatasource.HasDataSourcePermissions())
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourcesPermissions, DataSourcesPermissions);
        //                                }
        //                                else
        //                                {
        //                                    AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourcesPermissions, DataSourcesPermissions,true);
        //                                }
        //                            }
        //                            //if (sList[4] == DataSourceViews)
        //                            //{
        //                            //    AMO_Database_DataSourceview amodbdatasource = null;
        //                            //    amodbdatasource = new AMO_Database_DataSourceview();
        //                            //    amodbdatasource.SetAMODatabaseDataSource(amodb, sList[5]);
        //                            //    if (amodbdatasource.HasDataSourcePermissions())
        //                            //    {
        //                            //        AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourcesPermissions, DataSourcesPermissions);
        //                            //    }
        //                            //    else
        //                            //    {
        //                            //        AddTreeLevel(sList[1], ben, ben.Name + treeView1.PathSeparator + DataSourcesPermissions, DataSourcesPermissions, true);
        //                            //    }
        //                            //}
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        else
        //        {
        //            //Do nothing
        //        }
        //    }

        //}
        #region Treeview After Select
        
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            TreeNode ben = e.Node;
            string[] sList = pathlist(ben.FullPath);
            if (ben.Level == 0) //server level
            {
                //return list of server objects (SSAS / SSRS / SQL / SSIS / SSAS TAB
                Treeviewproperties_level_0(ben, sList);
            }
            else if (ben.Level == 1)
            {
                Treeviewproperties_level_1(ben, sList);
            }
            else if (ben.Level == 2)
            {
                Treeviewproperties_level_2(ben, sList);
            }
            else if (ben.Level == 3)
            {
                Treeviewproperties_level_3(ben, sList);
            }
            else if (ben.Level == 4)
            {
                Treeviewproperties_level_4(ben, sList);
            }
        }

        private Treeview_Properties SetTreeviewProperties_General(TreeNode ben)
        {
            Treeview_Properties tvp = new Treeview_Properties();
            tvp.winlist = new ListView();
            tvp.winlist.StateImageList = this.imageList1;
            tvp.winlist.SmallImageList = this.imageList1;
            tvp.winlist.LargeImageList = this.imageList1;
            tvp.key = ben.FullPath;
            tvp.winimagelist = this.imageList1;
            return tvp;
        }

        private void Treeviewproperties_level_0(TreeNode ben, string[] sList)
        {
            Treeview_Properties tvp =  SetTreeviewProperties_General(ben);
            tvp.winlist.Columns.Add("Server");
            tvp.winlist.Columns.Add("Serevr Info");
            tvp.imagekey = "Server.ico";
            foreach (string key in treelevel0.Keys)
            {
                ListViewItem lvi = new ListViewItem();
                lvi.Name = key;
                lvi.Text = key;
                if (treelevel0[key] is SMK_SSAS.AMO_Server)
                {
                    SMK_SSAS.AMO_Server amokey = (SMK_SSAS.AMO_Server)treelevel0[key];
                    amokey.amoserver().Refresh();
                    lvi.ImageKey = "Server_SSAS.ico";
                    lvi.SubItems.Add(amokey.ServerInfo());
                    tvp.winlist.Items.Add(lvi);
                }
                tvp.winlist.Dock = DockStyle.Fill;
                tvp.winlist.View = View.List;
                tvp.winlist.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }

        private void Treeviewproperties_level_1_AS(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Server_SSAS.ico";
            tvp.winlist.Columns.Add("Service Objects");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                if (sList[2] == DataBases)
                {
                    lvi.ImageKey = "Folder.ico";
                    tvp.winlist.Items.Add(lvi);
                }
                if (sList[2] == Sessions)
                {
                    lvi.ImageKey = "Activity.ico";
                    tvp.winlist.Items.Add(lvi);
                }
                if (sList[2] == Assemblies)
                {
                    lvi.ImageKey = "Folder.ico";
                    tvp.winlist.Items.Add(lvi);
                }
                if (sList[2] == Roles)
                {
                    lvi.ImageKey = "Role.ico";
                    tvp.winlist.Items.Add(lvi);
                } 
                if (sList[2] == Traces)
                {
                    lvi.ImageKey = "performance.ico";
                    tvp.winlist.Items.Add(lvi);
                } 
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
        private void Treeviewproperties_level_1(TreeNode ben, string[] sList)
        {
            Treeview_Properties tvp = SetTreeviewProperties_General(ben);
            if (ben.Nodes.Count == 0 || ben.Nodes[0].Name == "Dummy")
            {
                AddTreeLevel1(ben, sList);
            }

            if (ben.Nodes.Count > 0 && ben.Nodes[0].Name != "Dummy")
            {
                sList = pathlist(ben.Nodes[0].FullPath);
                if (sList[1] == AnalysisService)
                {
                    Treeviewproperties_level_1_AS(ben, sList, tvp);
                }
            }
        }

        private void Treeviewproperties_level_2(TreeNode ben, string[] sList)
        {
            Treeview_Properties tvp = SetTreeviewProperties_General(ben);
            //if (ben.Nodes.Count == 0 || ben.Nodes[0].Name == "Dummy")
            //{
                AddTreeLevel2(ben, sList);
            //}

            if (ben.Nodes.Count > 0 && ben.Nodes[0].Name != "Dummy")
            {
                sList = pathlist(ben.Nodes[0].FullPath);
                if (sList[1] == AnalysisService)
                {
                    Treeviewproperties_level_2_AS(ben, sList, tvp);
                }
            }
        }
        private void Treeviewproperties_level_2_AS(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            if(sList[2]== Sessions)
            {
                Treeviewproperties_level_2_AS_Sessions(ben, sList, tvp);
            }
            else if (sList[2] == Assemblies)
            {
                Treeviewproperties_level_2_AS_Assembly(ben, sList, tvp);
            }
            else if (sList[2] == DataBases)
            {
                Treeviewproperties_level_2_AS_Database(ben, sList, tvp);
            }
            else if (sList[2] == Roles)
            {
                Treeviewproperties_level_2_AS_Roles(ben, sList, tvp);
            }
            else if (sList[2] == Traces)
            {
                Treeviewproperties_level_2_AS_Traces(ben, sList, tvp);
            }
        }
        private void Treeviewproperties_level_2_AS_Sessions(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Activity.ico";
            tvp.winlist.Columns.Add("Users");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "user.ico";
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
        private void Treeviewproperties_level_2_AS_Assembly(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Folder.ico";
            tvp.winlist.Columns.Add("Assembly");
            tvp.winlist.Columns.Add("Description");
            tvp.winlist.Columns.Add("CreatedTimestamp");
            tvp.winlist.Columns.Add("LastSchemaUpdate");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "Assembly.ico";
                SMK_SSAS.AMO_Key amokey = (AMO_Key)treelevel1[tn.Name];
                if (amokey.LevelObject is Microsoft.AnalysisServices.Assembly)
                { 
                    Microsoft.AnalysisServices.Assembly assem = ( Microsoft.AnalysisServices.Assembly)amokey.LevelObject;
                    lvi.SubItems.Add(assem.Description);
                    lvi.SubItems.Add(assem.CreatedTimestamp.ToString());
                    lvi.SubItems.Add(assem.LastSchemaUpdate.ToString());
                    if (assem.IsLoaded)
                    {
                        lvi.BackColor = Color.Transparent;
                        lvi.ForeColor = Color.Black;
                    }
                    else
                    {
                        lvi.BackColor = Color.Red;
                        lvi.ForeColor = Color.White;
                    }
                }
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
        private void Treeviewproperties_level_2_AS_Database(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Folder.ico";
            tvp.winlist.Columns.Add("Database");
            tvp.winlist.Columns.Add("Description");
            tvp.winlist.Columns.Add("CreatedTimestamp");
            tvp.winlist.Columns.Add("LastSchemaUpdate");
            tvp.winlist.Columns.Add("LastProcessed");
            tvp.winlist.Columns.Add("EstimatedSize");
            tvp.winlist.Columns.Add("ModelType");
            tvp.winlist.Columns.Add("ProcessingPriority");
            tvp.winlist.Columns.Add("ReadWriteMode");
            tvp.winlist.Columns.Add("DbStorageLocation");
            tvp.winlist.Columns.Add("CompatibilityLevel");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "ASDatabase.ico";
                SMK_SSAS.AMO_Key amokey = (AMO_Key)treelevel1[tn.Name];
                if (amokey.LevelObject is SMK_SSAS.AMO_Database)
                {
                    SMK_SSAS.AMO_Database smkasdb = (SMK_SSAS.AMO_Database)amokey.LevelObject;
                    Microsoft.AnalysisServices.Database asdb = smkasdb.amodatabase();
                    asdb.Refresh();
                    lvi.SubItems.Add(asdb.Description);
                    lvi.SubItems.Add(asdb.CreatedTimestamp.ToString());
                    lvi.SubItems.Add(asdb.LastSchemaUpdate.ToString());
                    lvi.SubItems.Add(asdb.LastProcessed.ToString());
                    lvi.SubItems.Add(asdb.EstimatedSize.ToString());
                    lvi.SubItems.Add(asdb.ModelType.ToString());
                    lvi.SubItems.Add(asdb.ProcessingPriority.ToString());
                    lvi.SubItems.Add(asdb.ReadWriteMode.ToString());
                    if (asdb.DbStorageLocation == null)
                    {
                        lvi.SubItems.Add("Server Default");
                    }
                    else
                    {
                        lvi.SubItems.Add(asdb.DbStorageLocation);
                    }
                    lvi.SubItems.Add(asdb.CompatibilityLevel.ToString());
                    if (asdb.State== Microsoft.AnalysisServices.AnalysisState.Processed)
                    {
                        lvi.BackColor = Color.Green;
                        lvi.ForeColor = Color.White;
                    }
                    else if (asdb.State == Microsoft.AnalysisServices.AnalysisState.PartiallyProcessed)
                    {
                        lvi.BackColor = Color.Yellow;
                        lvi.ForeColor = Color.Black;
                    }
                    else
                    {
                        lvi.BackColor = Color.Red;
                        lvi.ForeColor = Color.White;
                    }
                }
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp); 
        }
        private void Treeviewproperties_level_2_AS_Roles(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Role.ico";
            tvp.winlist.Columns.Add("Role");
            tvp.winlist.Columns.Add("Description");
            tvp.winlist.Columns.Add("CreatedTimestamp");
            tvp.winlist.Columns.Add("LastSchemaUpdate");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "users.ico";
                SMK_SSAS.AMO_Key amokey = (AMO_Key)treelevel1[tn.Name];
                if (amokey.LevelObject is SMK_SSAS.AMO_Roles)
                {
                    SMK_SSAS.AMO_Roles smkamorole = (SMK_SSAS.AMO_Roles)amokey.LevelObject;
                    Microsoft.AnalysisServices.Role asrole = smkamorole.amorole;

                    lvi.SubItems.Add(asrole.Description);
                    lvi.SubItems.Add(asrole.CreatedTimestamp.ToString());
                    lvi.SubItems.Add(asrole.LastSchemaUpdate.ToString());
                    if (asrole.IsLoaded)
                    {
                        lvi.BackColor = Color.Transparent;
                        lvi.ForeColor = Color.Black;
                    }
                    else
                    {
                        lvi.BackColor = Color.Red;
                        lvi.ForeColor = Color.White;
                    }
                }
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
        private void Treeviewproperties_level_2_AS_Traces(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "performance.ico";
            tvp.winlist.Columns.Add("Trace");
            tvp.winlist.Columns.Add("Description");
            tvp.winlist.Columns.Add("CreatedTimestamp");
            tvp.winlist.Columns.Add("LastSchemaUpdate");
            tvp.winlist.Columns.Add("AutoRestart");
            tvp.winlist.Columns.Add("IsStarted");
            tvp.winlist.Columns.Add("LogFileAppend");
            tvp.winlist.Columns.Add("LogFileName");
            tvp.winlist.Columns.Add("LogFileRollover");
            tvp.winlist.Columns.Add("LogFileSize");
            tvp.winlist.Columns.Add("StopTime");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "Trace.ico";
                SMK_SSAS.AMO_Key amokey = (AMO_Key)treelevel1[tn.Name];
                if (amokey.LevelObject is AMO_Traces)
                {
                    AMO_Traces amot = (AMO_Traces)amokey.LevelObject;
                    Microsoft.AnalysisServices.Trace astrace = amot.amotrace;
                    lvi.SubItems.Add(astrace.Description);
                    lvi.SubItems.Add(astrace.CreatedTimestamp.ToString());
                    lvi.SubItems.Add(astrace.LastSchemaUpdate.ToString());
                    lvi.SubItems.Add(astrace.AutoRestart.ToString());
                    lvi.SubItems.Add(astrace.IsStarted.ToString());
                    lvi.SubItems.Add(astrace.LogFileAppend.ToString());
                    lvi.SubItems.Add(astrace.LogFileName);
                    lvi.SubItems.Add(astrace.LogFileRollover.ToString());
                    lvi.SubItems.Add(astrace.LogFileSize.ToString());
                    lvi.SubItems.Add(astrace.StopTime.ToString());
                    if (astrace.IsStarted)
                    {
                        lvi.ForeColor = Color.Gray;
                        lvi.BackColor = Color.LightCyan;
                    }
                    else
                    {
                        lvi.ForeColor = Color.Black;
                        lvi.BackColor = Color.Transparent;
                    }
                }
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
 
        private void Treeviewproperties_level_3(TreeNode ben, string[] sList)
        {
            Treeview_Properties tvp = SetTreeviewProperties_General(ben);
            //if (ben.Nodes.Count == 0 || ben.Nodes[0].Name == "Dummy")
            //{
            AddTreeLevel3(ben, sList);
            //}

            //if (ben.Nodes.Count > 0 && ben.Nodes[0].Name != "Dummy")
            //{
                sList = pathlist(ben.FullPath);
                if (sList[1] == AnalysisService)
                {
                    Treeviewproperties_level_3_AS(ben, sList, tvp);
                }
            //}
        }
        private void Treeviewproperties_level_3_AS(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            if (sList[2] == Sessions)
            {
                Treeviewproperties_level_3_AS_Sessions(ben, sList, tvp);
            }
            else if (sList[2] == Assemblies)
            {
                Treeviewproperties_level_3_AS_Assembly(ben, sList, tvp);
            }
            else if (sList[2] == DataBases)
            {
                Treeviewproperties_level_3_AS_Database(ben, sList, tvp);
            }
            else if (sList[2] == Roles)
            {
                Treeviewproperties_level_3_AS_Roles(ben, sList, tvp);
            }
            else if (sList[2] == Traces)
            {
                Treeviewproperties_level_3_AS_Traces(ben, sList, tvp);
            }
        }
        private void Treeviewproperties_level_3_AS_Sessions(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "user.ico";
            tvp.winlist.Columns.Add("SessionID");
            tvp.winlist.Columns.Add("CurrentDatabase");
            tvp.winlist.Columns.Add("StartTime");
            tvp.winlist.Columns.Add("SPID");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                AMO_Key amokey = null;
                AMO_SessionData sessiondata = null;
                if (treelevel2.ContainsKey(tn.Name))
                {
                    amokey = (AMO_Key)treelevel2[tn.Name];
                    if (amokey.LevelObject is AMO_SessionData)
                    {
                        sessiondata = (AMO_SessionData)amokey.LevelObject;
                    }
                }
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                if (sessiondata != null)
                {
                    lvi.SubItems.Add(sessiondata.SESSION_CURRENT_DATABASE);
                    lvi.SubItems.Add(sessiondata.SESSION_START_TIME.ToString());
                    lvi.SubItems.Add(sessiondata.SESSION_SPID.ToString());

                    if (amokey.IsActive)
                    {
                        lvi.ForeColor = Color.Gray;
                        lvi.BackColor = Color.LightCyan;
                    }
                    else
                    {
                        lvi.ForeColor = Color.Black;
                        lvi.BackColor = Color.Transparent;
                    }
                }
                lvi.ImageKey = "Session.ico";
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }

        private void Treeviewproperties_level_3_AS_Assembly(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
        }
        private void Treeviewproperties_level_3_AS_Database(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {

            tvp.imagekey = "ASDatabase.ico";
            tvp.winlist.Columns.Add("Name");
            tvp.winlist.Columns.Add("Count");
            foreach (TreeNode tn in ben.Nodes)
            {
                sList = pathlist(tn.FullPath);
                ListViewItem lvi = new ListViewItem();
                AMO_Key amokey = null;
                AMO_Database amodb= null;
                if (treelevel1.ContainsKey(ben.Name))
                {
                    amokey = (AMO_Key)treelevel1[ben.Name];
                    if (amokey.LevelObject is AMO_Database)
                    {
                        amodb = (AMO_Database)amokey.LevelObject;
                        if (sList[4] == Accounts)
                        { 
                            lvi.SubItems.Add(amodb.Accounts().Count.ToString());
                        }
                        if (sList[4] == Assemblies)
                        {
                            lvi.SubItems.Add(amodb.Assemblies().Count.ToString());
                        }
                        if (sList[4] == Cubes)
                        {
                            lvi.SubItems.Add(amodb.Cubes().Count.ToString());
                        }
                        if (sList[4] == DatabasePermissions)
                        {
                            lvi.SubItems.Add(amodb.DatabasePermissions().Count.ToString());
                        }
                        if (sList[4] == MiningStructures)
                        {
                            lvi.SubItems.Add(amodb.DatabaseMiningStructures().Count.ToString());
                        }
                        if (sList[4] == Translations)
                        {
                            lvi.SubItems.Add(amodb.DatabaseTranslations().Count.ToString());
                        }
                        if (sList[4] == DataSources)
                        {
                            lvi.SubItems.Add(amodb.DataSources().Count.ToString());
                        }
                        if (sList[4] == DataSourceViews)
                        {
                            lvi.SubItems.Add(amodb.DataSourceviews().Count.ToString());
                        }
                        if (sList[4] == Roles)
                        {
                            lvi.SubItems.Add(amodb.Roles().Count.ToString());
                        }

                        if (sList[4] == Dimensions)
                        {
                            lvi.SubItems.Add(amodb.DatabaseDimensions().Count.ToString());
                        }

                    }
                }
                lvi.Name = tn.Name;
                lvi.Text = tn.Text;
                lvi.ImageKey = "folder.ico";
                tvp.winlist.Items.Add(lvi);
            }
            gtvp = tvp;
            tvs(this, tvp);
        }
        private void Treeviewproperties_level_3_AS_Roles(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
        }
        private void Treeviewproperties_level_3_AS_Traces(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            if(treelevel1.ContainsKey(ben.FullPath))
            {
                AMO_Key amok = (AMO_Key)treelevel1[ben.FullPath];
                if (amok.LevelObject is AMO_Traces)
                {
                    AMO_Traces amot = (AMO_Traces)amok.LevelObject;
                    tvp.imagekey = "performance.ico";
                    tvp.winlist.Columns.Add("Trace");
                    tvp.winlist.Columns.Add("Description");
                    tvp.winlist.Columns.Add("CreatedTimestamp");
                    tvp.winlist.Columns.Add("LastSchemaUpdate");
                    tvp.winlist.Columns.Add("AutoRestart");
                    tvp.winlist.Columns.Add("IsStarted");
                    tvp.winlist.Columns.Add("LogFileAppend");
                    tvp.winlist.Columns.Add("LogFileName");
                    tvp.winlist.Columns.Add("LogFileRollover");
                    tvp.winlist.Columns.Add("LogFileSize");
                    tvp.winlist.Columns.Add("StopTime");
                    ListViewItem lvi = new ListViewItem();
                    lvi.Name = amot.amotrace.Name;
                    lvi.Text = amot.amotrace.Name;
                    lvi.ImageKey = "Trace.ico";
                    //Microsoft.AnalysisServices.Trace astrace = (Microsoft.AnalysisServices.Trace)amokey.LevelObject;
                    lvi.SubItems.Add(amot.amotrace.Description);
                    lvi.SubItems.Add(amot.amotrace.CreatedTimestamp.ToString());
                    lvi.SubItems.Add(amot.amotrace.LastSchemaUpdate.ToString());
                    lvi.SubItems.Add(amot.amotrace.AutoRestart.ToString());
                    lvi.SubItems.Add(amot.amotrace.IsStarted.ToString());
                    lvi.SubItems.Add(amot.amotrace.LogFileAppend.ToString());
                    lvi.SubItems.Add(amot.amotrace.LogFileName);
                    lvi.SubItems.Add(amot.amotrace.LogFileRollover.ToString());
                    lvi.SubItems.Add(amot.amotrace.LogFileSize.ToString());
                    lvi.SubItems.Add(amot.amotrace.StopTime.ToString());
                    if (amot.amotrace.IsStarted)
                    {
                        lvi.ForeColor = Color.Gray;
                        lvi.BackColor = Color.LightCyan;
                    }
                    else
                    {
                        lvi.ForeColor = Color.Black;
                        lvi.BackColor = Color.Transparent;
                    }
                    tvp.winlist.Items.Add(lvi);
                    gtvp = tvp;
                    tvs(this, tvp);
                }
            }
        }
        
        
        private void Treeviewproperties_level_4(TreeNode ben, string[] sList)
        {
            Treeview_Properties tvp = SetTreeviewProperties_General(ben);
            //if (ben.Nodes.Count == 0 || ben.Nodes[0].Name == "Dummy")
            //{
            AddTreeLevel4(ben, sList);
            //}

            if (ben.Nodes.Count > 0 && ben.Nodes[0].Name != "Dummy")
            {
                sList = pathlist(ben.Nodes[0].FullPath);
                if (sList[1] == AnalysisService)
                {
                    Treeviewproperties_level_4_AS(ben, sList, tvp);
                }
            }
            else if (ben.Name != "Dummy" && treelevel2.ContainsKey(ben.FullPath))
            {
                sList = pathlist(ben.FullPath);
                if (sList[1] == AnalysisService)
                {
                    Treeviewproperties_level_4_AS(ben, sList, tvp);
                }
            }

        }
        private void Treeviewproperties_level_4_AS(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            if (sList[2] == Sessions)
            {
                Treeviewproperties_level_4_AS_Sessions(ben, sList, tvp);
            }
            else if (sList[2] == Assemblies)
            {
                Treeviewproperties_level_4_AS_Assembly(ben, sList, tvp);
            }
            else if (sList[2] == DataBases)
            {
                Treeviewproperties_level_4_AS_Database(ben, sList, tvp);
            }
            else if (sList[2] == Roles)
            {
                Treeviewproperties_level_4_AS_Roles(ben, sList, tvp);
            }
            else if (sList[2] == Traces)
            {
                Treeviewproperties_level_4_AS_Traces(ben, sList, tvp);
            }
        }
        private void Treeviewproperties_level_4_AS_Sessions(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
            tvp.imagekey = "Session.ico";
            tvp.winlist.Columns.Add("SessionID");
            tvp.winlist.Columns.Add("CurrentDatabase");
            tvp.winlist.Columns.Add("StartTime");
            tvp.winlist.Columns.Add("Memory");
            tvp.winlist.Columns.Add("LastCommand");
            tvp.winlist.Columns.Add("SPID");

            ListViewItem lvi = new ListViewItem();
            AMO_Key amokey = null;
            AMO_SessionData sessiondata = null;
            if (treelevel2.ContainsKey(ben.Name))
            {
                amokey = (AMO_Key)treelevel2[ben.Name];
                if (amokey.LevelObject is AMO_SessionData)
                {
                    sessiondata = (AMO_SessionData)amokey.LevelObject;
                }
            }
            lvi.Name = ben.Name;
            lvi.Text = ben.Text;
            if (sessiondata != null)
            {
                lvi.SubItems.Add(sessiondata.SESSION_CURRENT_DATABASE);
                lvi.SubItems.Add(sessiondata.SESSION_START_TIME.ToString());
                lvi.SubItems.Add(sessiondata.SESSION_USED_MEMORY.ToString());
                lvi.SubItems.Add(sessiondata.SESSION_LAST_COMMAND);
                lvi.SubItems.Add(sessiondata.SESSION_SPID.ToString());
                if (amokey.IsActive)
                {
                    lvi.ForeColor = Color.Gray;
                }
                else
                {
                    lvi.ForeColor = Color.Black;
                }
            }
            lvi.ImageKey = "Session.ico";
            tvp.winlist.Items.Add(lvi);
            tvp.winlist.ContextMenuStrip = contextMenuStripASActivities;
            gtvp = tvp;
            tvs(this, tvp);
        }

        private void Treeviewproperties_level_4_AS_Assembly(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
        }
        private void Treeviewproperties_level_4_AS_Database(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {

        }
        private void Treeviewproperties_level_4_AS_Roles(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
        }
        private void Treeviewproperties_level_4_AS_Traces(TreeNode ben, string[] sList, Treeview_Properties tvp)
        {
        }
        #endregion




    }
}
